<?php

namespace app\api\controller;

use app\common\constant\driving\CommonConstant;
use app\common\controller\Api;
use app\common\service\driver\OrderReceiveService;
use think\Exception;

/**
 * 首页接口
 * @ApiWeigh (99)
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 三方渠道发单
     *
     * @ApiMethod (POST)
     * @ApiParams (name="cipherParam", type="string", required=true, description="加密参数")
     * @ApiParams (name="token", type="string", required=true, description="token")
     * @ApiParams (name="tranCode", type="string", required=true, description="code KZDJ")
     **/
    public function receive_order()
    {
        $params = $this->request->param();
        OrderReceiveService::receive_order($params, 'hichefu', __FUNCTION__);
    }

    /**
     * 取消订单
     *
     * @ApiMethod (POST)
     * @ApiParams (name="cipherParam", type="string", required=true, description="加密参数")
     * @ApiParams (name="token", type="string", required=true, description="token")
     * @ApiParams (name="tranCode", type="string", required=true, description="code KZDJ")
     **/
    public function cancel_order()
    {
        $params = $this->request->param();
        OrderReceiveService::receive_order($params, 'hichefu', __FUNCTION__);
    }

    /**
     * 订单详情
     *
     * @ApiMethod (POST)
     * @ApiParams (name="cipherParam", type="string", required=true, description="加密参数")
     * @ApiParams (name="token", type="string", required=true, description="token")
     * @ApiParams (name="tranCode", type="string", required=true, description="code KZDJ")
     **/
    public function order_detail()
    {
        $params = $this->request->param();
        OrderReceiveService::receive_order($params, 'hichefu', __FUNCTION__);
    }

    /**
     * 测试分配司机
     *
     * @ApiMethod (POST)
     * @ApiParams (name="status", type="integer", required=true, description="")
     * @ApiParams (name="out_order_id", type="string", required=true, description="")
     * @ApiParams (name="order_id", type="string", required=true, description="")
     * @ApiParams (name="amount", type="string", required=true, description="充值金额")
     * @ApiParams (name="paytype", type="string", required=true, description="支付类型:wechat=微信支付,alipay=支付宝支付")
     * @ApiParams (name="apptype", type="string", required=true, description="支付方法:app=APP支付,scan=扫码支付,miniapp=小程序支付")
     **/
    public function ceshi()
    {
        $status = $this->request->param('status');
        $out_order_id = $this->request->param('out_order_id');
        $order_id = $this->request->param('order_id');
        $amount = $this->request->post('amount');
        $paytype = $this->request->post('paytype');
        $apptype = $this->request->post('apptype');
        $order = compact('out_order_id', 'order_id');
        $params = [
            'amount' => $amount,
            'orderid' => $order_id,
            'type' => $paytype,
            'title' => "支付订单，{$amount}元",
            'method' => $apptype,
        ];
        $response = \app\api\controller\Pay::experience($params, 'order');
        $this->success('success', compact('paytype', 'apptype', 'response'));

        // 分配司机
        try {
            OrderReceiveService::distribute_order($order_id);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('success');

        // 订单状态更新
        try {
            $result = OrderReceiveService::driving('hichefu','updateOrderStatus',$status, $order);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('success', $result);
    }

}
